import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { Button, Input, Form } from "react-vant";
import { login } from "../../store/user/action";
import { useNavigate } from "react-router-dom";
import { RootState } from "../../store";

export default () => {
  const [form] = Form.useForm();
  const dispatch = useDispatch();
  const navigate = useNavigate();
  const token = useSelector((state: RootState) => state.user.token);

  const onFinish = (values: any) => {
    dispatch(login(values) as any);
  };
  if(token) {
    navigate('/')
  }

  return (
    <Form
      form={form}
      onFinish={onFinish}
      footer={
        <div style={{ margin: "16px 16px 0" }}>
          <Button round nativeType="submit" type="primary" block>
            提交
          </Button>
        </div>
      }
    >
      <Form.Item
        rules={[{ required: true, message: "请填写用户名" }]}
        name="username"
        label="用户名"
      >
        <Input placeholder="请输入用户名" />
      </Form.Item>
      <Form.Item
        rules={[{ required: true, message: "请填写密码" }]}
        name="password"
        label="密码"
      >
        <Input placeholder="请输入密码" />
      </Form.Item>
    </Form>
  );
};
